package Datos;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.CallableStatement;
import java.util.*;

public class DetalleOrdenVenta {
	Sesion oSesion;
	
	public DetalleOrdenVenta() {};
	
	public int Add(int idOrden, int idDetalle, Entidades.DetalleOrdenVenta item) throws Exception{
		try{
		Connection oCnn = Datos.General.CrearConexion();
		String storedProcedure = "{call cpd.DetalleOrdenVentaAdd(?,?,?)}";
		CallableStatement cs =oCnn.prepareCall(storedProcedure);
		poneDatos(idOrden, idDetalle, item, cs);
		boolean pipi = cs.execute();
		int numero = 0;
		if (pipi) numero = 1;
		return numero;
		
		} catch (Exception ex){
			   System.out.println("SQLException: " + ex.getMessage());
			   return -1;
		}
	}
	public void Update(Entidades.DetalleOrdenVenta item) throws Exception{
		try{
			Connection oCnn = Datos.General.CrearConexion();
			String storedProcedure = "{call cpd.DetalleOrdenVentaUpdate(?,?,?,?,?,?,?,?,?,?)}";
			CallableStatement cs =oCnn.prepareCall(storedProcedure);
			//poneDatos(item,cs);
			boolean pipi = cs.execute();
			return;
		} catch (Exception ex){
			   System.out.println("SQLException: " + ex.getMessage());
		}
	}
	public void Delete(String id) throws Exception{
		try{
			Connection oCnn = Datos.General.CrearConexion();
			String storedProcedure = "{call cpd.DetalleOrdenVentaDelete(?)}";
			CallableStatement cs =oCnn.prepareCall(storedProcedure);
			cs.setString("mIdDetalleOrdenVenta", id);

			boolean pipi = cs.execute();
			return;
		} catch (Exception ex){
			   System.out.println("SQLException: " + ex.getMessage());
		}
	}
	public List GetOne(String id) throws Exception{
		Statement stmt = null;   //
		ResultSet rs = null;	//
		Connection oCnn = Datos.General.CrearConexion();
		String storedProcedure = "{call cpd.DetalleOrdenVentaGetOne(?)}";
		CallableStatement cs = oCnn.prepareCall(storedProcedure);
		cs.setString("mIdDetalleOrdenVenta", id);
		rs = cs.executeQuery();
		return sacaDatos(rs);
	}
	public List GetAll() throws Exception{
		Statement stmt = null;   //
		ResultSet rs = null;	//
		Connection oCnn = Datos.General.CrearConexion();
		String storedProcedure = "{call cpd.DetalleOrdenVentaGetAll()}";
		CallableStatement cs = oCnn.prepareCall(storedProcedure);
		rs = cs.executeQuery();
		return sacaDatos(rs);
	}
	public List Consulta(int id) throws Exception{
		oSesion = new Sesion();
		ResultSet rs = null;	//
		String sql = "SELECT * FROM detalleordenventa WHERE idOrden=" + id + ";";
		rs = oSesion.getSesion().executeQuery(sql);
		return sacaDatos(rs);
	}
	public int ConsultaMaxIdDetalle(String sql) throws Exception{
		oSesion = new Sesion();
		ResultSet rs = null;	//
		rs = oSesion.getSesion().executeQuery(sql);
		boolean empty = rs.next();
		if (empty==true){
			System.out.println("No esta vacio....." +  rs.getInt("idDetalle") );
			return rs.getInt("idDetalle");
		} else {
			System.out.println("Esta vacio....." +  1);
			return 1;						
		}

	}
	public List Primero() throws Exception{
		return Trae("FIRST");
	}
	public List Siguiente() throws Exception{
		return Trae("NEXT");
	}
	public List Anterior() throws Exception{
		return Trae("PREV");
	}
	public List Ultimo() throws Exception{
		return Trae("LAST");
	}
	public void Abrir() throws Exception{
		oSesion = new Sesion();
		try{
			String sql = "HANDLER `detalleordenventa` OPEN";
			oSesion.getSesion().executeQuery(sql);
		} catch (Exception ex){
			System.out.println("SQLException: " + ex.getMessage());
		}
	}
	public void Cerrar() throws Exception{
		try{
			String sql = "HANDLER `detalleordenventa` CLOSE";
			oSesion.getSesion().executeQuery(sql);
		} catch (Exception ex){
			System.out.println("SQLException: " + ex.getMessage());
		}
	}
	public List Trae(String condicion) throws Exception{
		ResultSet rs = null;	//
		String sql = "HANDLER `detalleordenventa` READ `PRIMARY` " + condicion + "";
		rs = oSesion.getSesion().executeQuery(sql);  //
		return sacaDatos(rs);
	}
	
	private void poneDatos(int idOrden, int idDetalle, Entidades.DetalleOrdenVenta item, CallableStatement cs){
		try{
			cs.setInt("mIdOrden", idOrden);
			cs.setInt("mIdDetalle", idDetalle);
			cs.setInt("mIdCPD",(int) item.getCPD().getIdCPD());
		} catch (Exception ex){
			   System.out.println("SQLException: " + ex.getMessage());
		}
	}
	
	private List sacaDatos(ResultSet rs){
		List oDatos = new ArrayList();
		Entidades.DetalleOrdenVenta oDetalleOrdenVenta = null;
		try{
			while(rs.next()){
				//creo el objeto CPD del DetalleOrdenVenta
				List oCPDs = new ArrayList();
				Entidades.CPD oCPD = null;
				oCPDs = new Datos.CPD().GetOne(rs.getInt(3));
				while(!oCPDs.isEmpty()){
					oCPD = (Entidades.CPD) oCPDs.remove(0);
				}
				oDetalleOrdenVenta = new Entidades.DetalleOrdenVenta(oCPD);
				oDatos.add(oDetalleOrdenVenta);
				oDetalleOrdenVenta = null;
			}
		} catch (Exception ex){
			System.out.println("SQLException: " + ex.getMessage());
		}
		return oDatos;
	}
}
